<?php
if(!defined('IN_WOGU')) {
    exit('Access Denied');
}

Loader::loadUserClass('FrontController', false, 'admin');

class Admin_LoginController extends FrontController
{
    public function indexAction() {
        if(!empty($_SESSION[RlModel::USERID])) {
            $this->forward('index', 'index');
        }

        $this->_view->assign(array(
            'head_title' => $this->_lang['admin_login'],
            'body_style' => 'login_body'
        ));
        
        $this->_view->display('login');
    }
    
    public function dologinAction() {
        if(!empty($_SESSION[RlModel::USERID])) {
            exit('1');
        }
        
        $fl = Loader::loadUserModel('FailedLoginsModel', true, 'admin');
        $failed = $fl->getByIp(IP);
        $isexpire = $failed['lastvist'] < SYS_TIME - FailedLoginsModel::EXPIRE;
        if($failed && $failed['nums'] >= FailedLoginsModel::TRYNUMS && !$isexpire) {
            $expire = FailedLoginsModel::EXPIRE - (SYS_TIME - $failed['lastvist']);
            $status = array('code' => 2, 'expire' => $expire);
            exit(json_encode($status));
        }
        
        if(!$this->getRl()->login($_POST)) {
            if($failed) {
                $nums = $isexpire ? 1 : $failed['nums'] + 1;
                $fl->updateByIp(IP, $nums);
            } else {
                $nums = 1;
                $fl->insertByIp(IP, $nums);
            }
            
            $status = array('code' => 3, 'nums' => FailedLoginsModel::TRYNUMS - $nums);
            exit(json_encode($status));
        }
        
        exit('4');
    }
    
    public function qqAction() {
        if($_SESSION['qq']) {
            showmessage($this->_lang['qq_already_logined'], ADMIN_NAME);
        }
        
        $qq = Loader::loadUserClass('QQ', true, 'admin');
        $qq->qq_login(isset($_GET['inadmin']) ? true : false);
    }
    
    public function doqqAction() {
        $successurl = ADMIN_NAME;
        $failedurl = ADMIN_NAME . '?m=admin&c=login';
        if($_SESSION['qq']) {
            showmessage($this->_lang['qq_already_logined'], $failedurl);
        }
        
        $qq = Loader::loadUserClass('QQ', true, 'admin');
        $access_token = $qq->qq_callback();
        
        if(!$access_token) {
            showmessage($this->_lang['qq_login_failed'], $failedurl);
        }
        
        $openid = $qq->get_openid($access_token);
        //如果已登陆，则QQ与已登陆帐号绑定
        if(!empty($_SESSION[RlModel::USERID])) {
            $member = $this->getRl()->getAdmin(array('userid' => $_SESSION[RlModel::USERID]));
            if(!empty($member['qq_openid']) && $member['qq_openid'] != $openid && $_SESSION[RlModel::FORCE_QQ] == 1) {
                $this->getRl()->qqLogout();
                showmessage($this->_lang['qq_unbind_first'], $failedurl);
            }
            
            $this->getRl()->updateAdmin(array('qq_openid' => $openid), array('userid' => $_SESSION[RlModel::USERID]));
            showmessage($this->_lang['qq_bind_success'], $successurl);
        }
        
        //如果未登陆
        //如果openid已经存在于库中，则直接登陆该帐号
        $member = $this->getRl()->getAdmin(array('qq_openid' => $openid));
        if($member) {
            $this->getRl()->setCookieSession($member);
            showmessage($this->_lang['login_success'], $successurl);
        }
        
        $this->getRl()->qqLogout();
        showmessage($this->_lang['admin_not_exist'], $failedurl);
    }
    
    public function logoutAction() {
        $this->getRl()->logout();
        showmessage($this->_lang['logout_success'], ADMIN_NAME);
    }
}